import Mock from 'mockjs'
import {param2Obj} from '@/utils'

const List = [];
const MemberList = [];
const AccountList = [];
const count = 4;

const departmentList = [
  {title: '客服部', value: 1},
  {title: '产品部', value: 2},
  {title: '超级组', value: 3},
];
const state = [
  {title: '正常', value: 1},
  {title: '锁定', value: 2},
];

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    'id|1': _.map(departmentList, 'value'),
    createTime: +Mock.Random.date('T'),
    lastModifyTIme: +Mock.Random.date('T'),
    'department|1': _.map(departmentList, 'title'),
    "count|1-100": 100,
    'status|1': _.map(state, 'title'),
    comment: Mock.Random.ctitle(20),
    'children|0-4': _.times(10, () => {
      return {
        'parentId|1': _.map(departmentList, 'value'),
        'id|1': _.map(departmentList, 'value'),
        createTime: +Mock.Random.date('T'),
        lastModifyTIme: +Mock.Random.date('T'),
        'department|1': _.map(departmentList, 'title'),
        "count|1-100": 100,
        'status|1': _.map(state, 'title'),
        comment: Mock.Random.ctitle(20),
      };
    })
  }))
}

for (let i = 0; i < 4; i++) {
  MemberList.push(Mock.mock({
    id: '@increment',
    'department|1': _.map(departmentList, 'title'),
    'children|0-10': _.times(20, () => {
      return {
        id: '@increment',
        name: Mock.mock('@first'),
      };
    })
  }))
}

for (let i = 0; i < 49; i++) {
  AccountList.push(Mock.mock({
    id: '@increment',
    createTime: +Mock.Random.date('T'),
    lastModifyTIme: +Mock.Random.date('T'),
    username: Mock.mock('@first'),
    'departmentId|1': _.map(departmentList, 'value'),
    'work|1': _.map(departmentList, 'title'),
    'department|1': _.map(departmentList, 'title'),
    'workId|1': _.map(departmentList, 'value'),
    "count|1-100": 100,
    'status|1': _.map(state, 'title'),
    modifier: Mock.mock('@first'),
    comment: Mock.Random.ctitle(20)
  }))
}

const InterfaceList = [];

for (let i = 0; i < 6; i++) {
  InterfaceList.push(Mock.mock({
    id: '@increment',
    url: Mock.Random.url(),
    template: Mock.Random.cparagraph(),
    "open|1-2": true
  }))
}

const permissionList = [{
  id: 1,
  label: '首页',
  enable: true,
  children: [{
    id: 4,
    label: '首页功能',
    auths: [
      {
        id: 1,
        title: '查看',
        enable: true
      },
      {
        id: 2,
        title: '操作',
        enable: true
      },
    ],
  }]
}, {
  id: 2,
  label: '玩家管理',
  children: [{
    id: 5,
    label: '当前在线玩家'
  }, {
    id: 6,
    label: '所有玩家列表',
    children: [{
      id: 3,
      label: '所有玩家列表',
      auths: [
        {
          id: 1,
          title: '查看',
          enable: true
        },
        {
          id: 2,
          title: '操作',
          enable: true
        },
      ]
    }, {
      id: 4,
      label: '导出玩家列表',
      auths: [
        {
          id: 1,
          title: '查看',
          enable: true
        },
        {
          id: 2,
          title: '操作',
          enable: true
        },
      ]
    }]
  }]
}];

export default {
  [api.AUTH_DEPARTMENT_LIST]: () => ({
    code: 1,
    data: List
  }),
  [api.AUTH_DEPARTMENT_MEMBER_LIST]: () => ({
    code: 1,
    data: MemberList,
  }),
  [api.AUTH_DEPARTMENT_DETAIL]: () => ({
    code: 1,
    data: {
      id: 3,
      departmentName: '超级部门',
      workNameList: [
        {
          id: 2,
          name: '测试'
        },
        {
          id: 12,
          name: '测试2'
        }
      ],
      desc: Mock.Random.cparagraph(),
      sort: 4,
      totalSort: 10
    }
  }),

  [api.AUTH_ACCOUNT_LIST]: config => {
    const {username, department, startTime, endTime, ip, page = 1, limit = 20} = param2Obj(config.body);

    let mockList = AccountList.filter(item => {
      // if (importance && item.importance !== +importance) return false;
      if (ip && item.ip !== ip) return false;
      if (username && item.username !== username) return false;
      if (department && item.department !== _.find(departmentList, {value: Number(department)}).title) return false;
      if (startTime && !moment(startTime).isBefore(item.opTime)) return false;
      if (endTime && !moment(endTime).isAfter(item.opTime)) return false;

      return true;
    });

    const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1));

    return {
      data: {
        total: mockList.length,
        items: pageList
      },
      code: 1,
    };
  },

  [api.AUTH_PERMISSION_LIST]: () => {
    return {
      data: permissionList,
      code: 1,
    };
  }
}
